package models

import (
	"github.com/gin-gonic/gin"
	"time"
)

type UserRecharges struct {
	ID int `gorm:"primary_key"`
	UserId int
	Money float64
	VipFeeConfigId int
	LastMoney float64
	OveredAt time.Time
	LastAt time.Time
	VipFeeConfigComment string
	IsPay int
	PayTime time.Time
	Appid string
	MchId string
	NonceStr string
	Sign string
	OutTradeNo string
	PayType string
	WeixinTotalFee float64
	Status int
	CreatedAt time.Time
	UpdatedAt time.Time
}


func (u *UserRecharges) TableName () string {
	return "user_recharges"
}

type UserRechargeBase struct {
	ID int `json:"id"`
	Name string `json:"name"`
	Phone string `json:"phone"`
	VipLevel int `json:"vip_level"`
	PayTime time.Time `json:"pay_time"`
	PayLength string `json:"pay_long"`
	WechatName string `json:"-"`
	UserName string `json:"-"`
	UserId int `json:"user_id"`
}




func UserRechargeList (page int, pageSize int, c *gin.Context) (interface{}, *Paginator){
	var res []*struct {
		UserRechargeBase
		Money float32 `json:"amount"`
		City string `json:"city"`
		Overdue time.Time `json:"overdue"`
		Status int `json:"status"`
		PayType string `json:"pay_type"`

	}
	m := DB.Table("user_recharges as ur").
		Select("ur.id,ur.user_id, ud.wechat_name,c.user_name, u.phone, v.type as vip_level,ur.pay_time, ur.vip_fee_config_comment as pay_length, ur.money,p.name as city, ur.overed_at as overdue, ur.pay_type").
		Joins("left join user_detail as ud on ud.user_id=ur.user_id").
		Joins("left join users as u on u.id=ur.user_id").
		Joins("left join vip_fee_config as v on v.id=ur.vip_fee_config_id").
		Joins("left join cards as c on c.user_id=ur.user_id").
		Joins("left join places as p on p.id=c.city").
		Where("ur.is_pay=1")


	if id, isExist := c.GetQuery("user_id"); isExist && id != "" {
		m = m.Where("ur.user_id=?", id)
	}
	if length, isExist := c.GetQuery("length"); isExist && length != "" {
		m = m.Where("ur.vip_fee_config_comment like ?", "%"+length+"%")
	}
	if status, isExist := c.GetQuery("status"); isExist && status != "" {
		m = m.Where("u.is_enabled=?", status)
	}
	if city, isExist := c.GetQuery("city"); isExist && city != "" {
		var id, level int
		DB.Table("places").Select("id,level").Where("name like ?", "%"+city+"%").Where("level <> 1").Row().Scan(&id,&level)
		if level == 2 {
			m = m.Where("c.city=?", id)
		}
		if level == 3 {
			m = m.Where("c.area=?", id)
		}
	}
	if payTimeStart, isExist := c.GetQuery("pay_time_start"); isExist && payTimeStart != "" {
		m = m.Where("ur.pay_time > ?", payTimeStart)
	}
	if payTimeEnd, isExist := c.GetQuery("pay_time_end"); isExist && payTimeEnd != "" {
		m = m.Where("ur.pay_time < ?", payTimeEnd)
	}
	if overdueStart, isExist := c.GetQuery("overdue_start"); isExist && overdueStart != "" {
		m = m.Where("ur.overed_at > ?", overdueStart)
	}
	if overdueEnd, isExist := c.GetQuery("overdue_end"); isExist && overdueEnd != "" {
		m = m.Where("ur.overed_at < ?", overdueEnd)
	}
	if payType, isExist := c.GetQuery("pay_type"); isExist && payType != "" {
		if payType == "管理员"{
			payType = "平台"
		}
		m = m.Where("ur.pay_type like ?", "%"+payType+"%")
	}
	if phone, isExist := c.GetQuery("phone"); isExist && phone != "" {
		m = m.Where("u.phone=?", phone)
	}
	var count int
	m.Count(&count)
	paginate := NewPage(page, pageSize, count)
	m.Offset(paginate.Offset).Limit(paginate.Limit).Find(&res)

	for _, item := range res{
		if item.UserName != "" {
			item.Name = item.UserName
		}else{
			item.Name = item.WechatName
		}
	}

	return res, &paginate
}



func UserProfitList(page int, pageSize int, c *gin.Context) (interface{}, *Paginator) {
	var res []*struct {
		UserRechargeBase
		Reward float32 `json:"reward"`
		PutForward float32 `json:"put_forward"`
		UnPutForward float32 `json:"un_put_forward"`
	}
	m := DB.Table("user_commission_details as uc").
		Select("uc.id,uc.relation_id as user_id, ud.wechat_name, c.user_name, u.phone, v.type as vip_level,ur.pay_time, ur.vip_fee_config_comment as pay_length, ur.money,c.city, ur.overed_at as overdue,sum(uc.money) as reward, u.money as un_put_forward,p.pu as put_forward").
		Joins("left join user_recharges as ur on ur.user_id=uc.relation_id and ur.overed_at > NOW()").
		Joins("left join user_detail as ud on ud.user_id=uc.relation_id").
		Joins("left join users as u on u.id=uc.relation_id").
		Joins("left join vip_fee_config as v on v.id=ur.vip_fee_config_id").
		Joins("left join cards as c on c.user_id=uc.relation_id").
		Joins("left join (SELECT sum(amount) as pu,user_id from put_forward where status=1 GROUP BY user_id) as p on p.user_id=uc.relation_id").
		Group("uc.relation_id")


	if length, isExist := c.GetQuery("length"); isExist && length != "" {
		m = m.Where("ur.vip_fee_config_comment like ?", "%"+length+"%")
	}
	if name, isExist := c.GetQuery("name"); isExist && name != "" {
		m = m.Where("ud.wechat_name like ? or c.user_name like ?", "%"+name+"%", "%"+name+"%")
	}
	if phone, isExist := c.GetQuery("phone"); isExist && phone != "" {
		m = m.Where("u.phone=?", phone)
	}
	if payTimeS, isExist := c.GetQuery("start_time"); isExist && payTimeS != "" {
		m = m.Where("ur.pay_time > ?", payTimeS)
	}
	if payTimeE, isExist := c.GetQuery("end_time"); isExist && payTimeE != "" {
		m = m.Where("ur.pay_time < ?", payTimeE)
	}
	var count int
	m.Count(&count)
	paginate := NewPage(page, pageSize, count)
	m.Offset(paginate.Offset).Limit(paginate.Limit).Find(&res)
	for _, item := range res{
		if item.UserName != "" {
			item.Name = item.UserName
		}else{
			item.Name = item.WechatName
		}
	}
	return res, &paginate
}

func UserProfitDetail (page int, pageSize int, userId int, c *gin.Context) (interface{}, *Paginator){
	var res []*struct{
		ID int `json:"id"`
		Name string `json:"name"`
		UserName string `json:"-"`
		WechatName string `json:"-"`
		Phone string `json:"phone"`
		Level string `json:"level"`
		PayTime time.Time `json:"pay_time"`
		PayLong string `json:"pay_long"`
		Amount float32 `json:"amount"`
		Reward float32 `json:"reward"`
		ExtractAt time.Time `json:"extract_at"`
	}

	m := DB.Table("user_commission_details as uc").
		Select("uc.id,c.user_name,ud.wechat_name,u.phone,uc.relation as level,ur.pay_time,ur.vip_fee_config_comment as pay_long,uc.money as reward,ur.money as amount,uc.updated_at as extract_at").
		Joins("left join users as u on u.id=uc.user_id").
		Joins("left join user_recharges as ur on ur.out_trade_no=uc.out_trade_no").
		Joins("left join user_detail as ud on ud.user_id=uc.user_id").
		Joins("left join cards as c on c.user_id=uc.user_id").
		Where("uc.relation_id=?", userId)

	if level, isExist := c.GetQuery("level"); isExist && level != "" {
		m = m.Where("uc.relation=?",level)
	}
	if payTimeS, isExist := c.GetQuery("pay_time_start"); isExist && payTimeS != "" {
		m = m.Where("ur.pay_time > ?", payTimeS)
	}
	if payTimeE, isExist := c.GetQuery("pay_time_end"); isExist && payTimeE != "" {
		m = m.Where("ur.pay_time < ?", payTimeE)
	}
	if extractTimeS, isExist := c.GetQuery("extract_time_start"); isExist && extractTimeS != "" {
		m = m.Where("uc.updated_at > ?", extractTimeS)
	}
	if extractTimeE, isExist := c.GetQuery("extract_time_end"); isExist && extractTimeE != "" {
		m = m.Where("uc.updated_at < ?", extractTimeE)
	}
	var count int
	m.Count(&count)
	paginate := NewPage(page, pageSize, count)
	m.Offset(paginate.Offset).Limit(paginate.Limit).Find(&res)
	return res, &paginate
}

func UserPutForwardList(page int, pageSize int, userId int, c *gin.Context) (interface{}, *Paginator){
	var res []*struct{
		ID int `json:"id"`
		BankName string `json:"bank_name"`
		BankCardNo string `json:"bank_card_no"`
		Name string `json:"name"`
		Amount float32 `json:"amount"`
		ForwardAt time.Time `json:"time"`
	}
	m := DB.Table("put_forward").Where("user_id=?", userId)
	if bt, isExist := c.GetQuery("begin_time"); isExist && bt != "" {
		m = m.Where("forward_at > ?", bt)
	}
	if et, isExist := c.GetQuery("end_time"); isExist && et != "" {
		m = m.Where("forward_at < ?", et)
	}
	var count int
	m.Count(&count)
	paginate := NewPage(page, pageSize, count)
	m.Offset(paginate.Offset).Limit(paginate.Limit).Find(&res)
	return res, &paginate
}